package com.powernode.controller;

import com.powernode.dto.PageResult;
import com.powernode.dto.Result;
import com.powernode.entity.IndexContent;
import com.powernode.exception.BizException;
import com.powernode.param.IndexContentQueryParam;
import com.powernode.service.IndexContentService;
import com.powernode.validation.UpdateGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/cms/admin/indexContents")
public class AdminIndexContentController {
    @Autowired
    private IndexContentService indexContentService;

    @GetMapping
    public Result<PageResult<IndexContent>> list(@Validated IndexContentQueryParam param) {
        return Result.success(indexContentService.pageQuery(param));
    }

    @PostMapping
    public Result save(@RequestBody @Validated IndexContent entity){
        if (!indexContentService.saveIndexContent(entity)) {
            throw new BizException(HttpStatus.BAD_REQUEST.value(), "新增首页内容失败");
        }
        return Result.success("新增首页内容成功",entity.getId());
    }

    @PutMapping
    public Result update(@RequestBody @Validated(UpdateGroup.class) IndexContent entity){
        if (!indexContentService.updateIndexContent(entity)) {
            throw new BizException(HttpStatus.BAD_REQUEST.value(), "修改首页内容失败");
        }
        return Result.success("修改首页内容成功",null);
    }

    @DeleteMapping("/{indexContentId}")
    public Result remove(@PathVariable Long indexContentId){
        if (!indexContentService.removeIndexContent(indexContentId)) {
            throw new BizException(HttpStatus.BAD_REQUEST.value(), "删除首页内容失败");
        }
        return Result.success("删除首页内容成功",null);
    }
}
